package com.jgame.secondarymarket.interfaces.api.product;

import com.jgame.secondarymarket.application.service.product.ProductApplicationService;
import com.jgame.secondarymarket.infrastructure.common.model.response.Result;
import com.jgame.secondarymarket.interfaces.assembler.product.ProductAssembler;
import com.jgame.secondarymarket.interfaces.dto.product.ProductDTO;
import com.jgame.secondarymarket.interfaces.query.page.product.PageProductQuery;
import com.jgame.secondarymarket.interfaces.query.product.UpdateProductQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @title: ProductApi
 * @Author Wangsj
 * @Date: 2021/7/21
 */
@RestController
@RequestMapping("product")
@Api(tags = {"商品接口"})
public class ProductApi {

    @Autowired
    ProductApplicationService productApplicationService;

    @ApiOperation(value = "新增商品")
    @PostMapping
    public Result save(@Validated @RequestBody ProductDTO product) {

        return Result.op(() -> productApplicationService.save(ProductAssembler.toProduct(product)));
    }

    @ApiOperation(value = "获取商品详情")
    @GetMapping("/{productId}")
    public Result get(@PathVariable Long productId) {
        return Result.ok(productApplicationService.findById(productId));
    }

    @ApiOperation(value = "修改商品信息")
    @PutMapping("/{productId}")
    public Result update(@PathVariable Long productId, @Validated @RequestBody UpdateProductQuery query) {
        return Result.op(() -> productApplicationService.update(query, productId));
    }

    @ApiOperation(value = "分页获取商品信息")
    @GetMapping("")
    public Result getPage(@Validated PageProductQuery query) {
        return Result.ok(productApplicationService.getPage(query));
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping("/{productId}")
    public Result del(@PathVariable Long productId) {
        return Result.op(() -> productApplicationService.delById(productId));
    }
}
